﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.DanhMuc;
using TGB.Core.Infrastructure;

namespace TGBTech.Web.Controllers
{
    public class KhachHangController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: /KhachHang/
        public ActionResult Index()
        {
            var list = db.KhachHang.Where(p => p.State == true).ToList();
            return View(list);
        }

        // GET: /KhachHang/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            KhachHang khachhang = db.KhachHang.Find(id);
            if (khachhang == null)
            {
                return HttpNotFound();
            }
            return View(khachhang);
        }

        // GET: /KhachHang/Create
        public ActionResult Create()
        {
            return PartialView();
        }

        // POST: /KhachHang/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Name,Address,Phone,Fax,MST,State")] KhachHang khachhang)
        {
            if (ModelState.IsValid)
            {
                khachhang.State = true;
                db.KhachHang.Add(khachhang);
                db.SaveChanges();
                return Json(new { Id = khachhang.Id, Name = khachhang.Name,
                                  Address = khachhang.Address,
                                  Phone = khachhang.Phone, Fax = khachhang.Fax,
                                  MST = khachhang.MST
                });
            }

            return PartialView(khachhang);
        }

        // GET: /KhachHang/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            KhachHang khachhang = db.KhachHang.Find(id);
            if (khachhang == null)
            {
                return HttpNotFound();
            }
            return PartialView(khachhang);
        }

        // POST: /KhachHang/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,Name,Address,Phone,Fax,MST,State")] KhachHang khachhang)
        {
            if (ModelState.IsValid)
            {
                db.Entry(khachhang).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(khachhang);
        }

        // GET: /KhachHang/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            KhachHang khachhang = db.KhachHang.Find(id);
            if (khachhang == null)
            {
                return HttpNotFound();
            }
            return PartialView(khachhang);
        }

        // POST: /KhachHang/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            KhachHang khachhang = db.KhachHang.Find(id);
            khachhang.State = false;
            db.Entry(khachhang).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
